package com.goods.business.service.impl;

import com.goods.business.mapper.IProductCategoryMapper;
import com.goods.business.service.IProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA
 *
 * @Date: 2023/3/6 - 10:31
 * @Author: Hyoung
 * @Description:
 */
@Service
public class ProductCategoryImpl implements IProductCategoryService {

    @Autowired
    private IProductCategoryMapper iProductCategoryMapper;

    @Override
    public void updateProductCategoryById(Integer id, ProductCategoryVO productCategoryVO) {
        productCategoryVO.setId(Long.valueOf(id));
        productCategoryVO.setModifiedTime(new Date());
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO, productCategory);
        iProductCategoryMapper.updateByPrimaryKey(productCategory);
    }

    @Override
    public ProductCategoryVO findProductCategoryById(Integer id) {
        ProductCategory productCategory = iProductCategoryMapper.selectByPrimaryKey(id);
        ProductCategoryVO productCategoryVO = new ProductCategoryVO();
        BeanUtils.copyProperties(productCategory, productCategoryVO);
        return productCategoryVO;

    }

    @Override
    @SneakyThrows
    public void delete(Integer id) {
        // 父id
        ProductCategory productCategory = iProductCategoryMapper.selectByPrimaryKey(id);
        //  删除父节点的时候不能删除子节点
        Example example = new Example(ProductCategory.class);
        example.createCriteria().andEqualTo("pid", productCategory.getId());
        List<ProductCategory> productCategorieChildList = iProductCategoryMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(productCategorieChildList)) {
            productCategorieChildList.forEach(productCategoryChild -> {
                this.delete(Math.toIntExact(productCategoryChild.getId()));
            });
        }
        iProductCategoryMapper.deleteByPrimaryKey(id);

    }

    @Override
    public void addProductCategory(ProductCategoryVO productCategoryVO) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setCreateTime(new Date());
        productCategory.setModifiedTime(new Date());
        BeanUtils.copyProperties(productCategoryVO, productCategory);
        iProductCategoryMapper.insert(productCategory);
    }

    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        List<ProductCategory> productCategories = iProductCategoryMapper.selectAll();
        // 将数据转成vo
        List<ProductCategoryTreeNodeVO> collect = productCategories.stream().map(productCategory -> {
            ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(productCategory, productCategoryTreeNodeVO);
            return productCategoryTreeNodeVO;
        }).collect(Collectors.toList());
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = CategoryTreeBuilder.buildParent(collect);
        return productCategoryTreeNodeVOS;
    }

    @Override
    public PageVO<ProductCategoryTreeNodeVO> findAll(Integer pageNum, Integer pageSize) {
        // 查询所有数据
        List<ProductCategory> productCategories = iProductCategoryMapper.selectAll();
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVo = productCategories.stream().map(productCategory -> {
            ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(productCategory, productCategoryTreeNodeVO);
            return productCategoryTreeNodeVO;
        }).collect(Collectors.toList());
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = CategoryTreeBuilder.build(productCategoryTreeNodeVo);
        if (Objects.isNull(pageSize)) {
            pageSize = productCategoryTreeNodeVo.size();
        }
        List<ProductCategoryTreeNodeVO> page = ListPageUtils.page(productCategoryTreeNodeVOList, pageSize, pageNum);
        // TODO分页动态页面有问题
        return new PageVO<>(productCategoryTreeNodeVOList.size(), page);
    }
}
